<template>
	<view class="login-btn">
		<button class="btn" :plain="true" open-type="getUserInfo" @click="startGet" @getuserinfo="getUserInfo"><slot></slot></button>
		<!-- <button class="btn" :plain="true" open-type="getPhoneNumber" @getphonenumber="getUserInfo"><slot></slot></button> -->
	</view>
</template>

<script>
export default {
	data() {
		return {};
	},
	methods: {
		startGet() {
			uni.showLoading({
				title: '加载中...',
				mask: true
			});
		},
		getUserInfo(even) {
			// 获取用户信息回调
			console.log('获取用户信息回调', even);
			const data = even.detail;
			if (even.detail.errMsg == 'getUserInfo:ok') {
				// 用户允许登录
				this.$store.dispatch('mpLogin').then(res => {
					//小程序登录
					this.$store
						.dispatch('sysLogin', {
							//系统登录
							encryptedData: data.encryptedData,
							iv: data.iv
						})
						.then(res => {
							console.log('登录成功', res);
							let data = res.data.data;
							this.$emit('loginSuccess', data);
							
							setTimeout(()=>{
								uni.showToast({
									icon:"success",
									mask:true,
									duration: 2000,
									title: '登录成功'
								});
							},500)
						
						});
				});
			} else {
				// 用户反对登录
				uni.hideLoading();
				uni.showToast({
					icon:"none",
					mask:true,
					duration: 2000,
					title: '登录失败'
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.login-btn {
	display: block;
	width: 100%;
	height: 100%;
	.btn {
		display: flex;
		align-items: center;
		width: 100%;
		height: 100%;
		border: 0;
	}
}
</style>
